package com.atguigu.dga.governance.assessor.storage;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga.constant.DgaConstant;
import com.atguigu.dga.governance.assessor.Assessor;
import com.atguigu.dga.governance.bean.AssessParam;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Description: 生命周期是否合理指标
 * 未设定周期类型的 给 0分
 * 周期类型为永久、拉链表 则给10分
 * 周期类型为日分区 :
 * 无分区信息的给0分 没设生命周期给0分
 * 周期长度超过建议周期天数{days}，则给 （建议周期天数  / 设置周期天数 ）*10
 * @Author: lay
 * @Date: 2024/6/28 20:34
 */
@Component("TABLE_LIFECYCLE")
public class TableLifecycleAssessor extends Assessor {
    @Override
    public void checkProblem(GovernanceAssessDetail governanceAssessDetail, AssessParam assessParam) throws Exception {
        //生周期类型
        String lifecycleType = assessParam.getTableMetaInfo().getTableMetaInfoExtra().getLifecycleType();
        //分区
        String partitionColNameJson = assessParam.getTableMetaInfo().getPartitionColNameJson();
        List<JSONObject> partitionColNameList = JSON.parseArray(partitionColNameJson, JSONObject.class);
        //生命周期天数
        Long lifecycleDays = assessParam.getTableMetaInfo().getTableMetaInfoExtra().getLifecycleDays();

        String paramsJson = assessParam.getGovernanceMetric().getMetricParamsJson();
        JSONObject jsonObject = JSONObject.parseObject(paramsJson);
        Integer days = jsonObject.getInteger("days");

        //未设置生命周期类型
        if (lifecycleType == null || lifecycleType.equals(DgaConstant.LIFECYCLE_TYPE_UNSET)){
            governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
            governanceAssessDetail.setAssessProblem("未设置生命周期类型");
            return;
        }
        //日分区
        if (lifecycleType.equals(DgaConstant.LIFECYCLE_TYPE_DAY)){
            if (partitionColNameJson != null){
                //可能是个 []
                if (partitionColNameList.size() == 0){
                    governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
                    governanceAssessDetail.setAssessProblem("未设置分区信息");
                }
            } else {
                governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
                governanceAssessDetail.setAssessProblem("未设置分区信息");
            }
            if (lifecycleDays == null || lifecycleDays == -1L){
                governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
                governanceAssessDetail.setAssessProblem("未设置生命周期天数");
            }
            if (lifecycleDays > days){
                //给分
                governanceAssessDetail.setAssessScore( BigDecimal.valueOf(  days * 10L  / lifecycleDays ));
                //问题项
                governanceAssessDetail.setAssessProblem("日分区表设定的生命周期天数大于建议值");
                //考评备注
                governanceAssessDetail.setAssessComment("建议天数: " + days + " , 实际天数: " + lifecycleDays);
            }
        }
        


    }
}
